//
//  ListView.m
//  DropDownList
//
//  Created by yuanyuan on 14-10-9.
//  Copyright (c) 2014年 CaoMao. All rights reserved.
//

#import "ListView.h"
#import "QuartzCore/QuartzCore.h"

@implementation ListView

//- (id)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        // Initialization code
//        
//    }
//    return self;
//}

-(id)ShowListView:(UIButton *)button andFloat:(CGFloat)heigh andArray:(NSArray *)array{
    if (self == [super init]) {
        self.btn = button;
        CGRect f = button.frame;
        self.frame = CGRectMake(f.origin.x, f.origin.y+f.size.height, f.size.width, heigh);
        
        self.list = [NSArray arrayWithArray:array];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, button.frame.size.width, 0)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.layer.cornerRadius = 5;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorColor = [UIColor clearColor];
        
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        self.frame = CGRectMake(f.origin.x, f.origin.y+f.size.height, f.size.width, heigh);
        self.tableView.frame = CGRectMake(0, 0, f.size.width, heigh);
        [UIView commitAnimations];
        
        [button.superview addSubview:self];
        [self addSubview:self.tableView];
    }
    return self;
}

-(void)hidenListView:(UIButton *)button{
    CGRect btn = button.frame;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0) ;
    self.tableView.frame = CGRectMake(0, 0, btn.size.width, 0);
    [UIView commitAnimations];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 20;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *IdentifityCell = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IdentifityCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:IdentifityCell];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    cell.textLabel.text = [self.list objectAtIndex:indexPath.row];
//    cell.textLabel.textColor = [UIColor blueColor];
//    cell.textLabel.font = [UIFont fontWithName:nil size:10];
    //设置下拉菜单中选中某个选项后的颜色设置
    UIView *vie = [[UIView alloc] init];
    vie.backgroundColor = [UIColor colorWithRed:175/255.0 green:175/255.0 blue:175/255.0 alpha:0.5];
    cell.selectedBackgroundView = vie;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self hidenListView:self.btn];
    UITableViewCell *ce = [tableView cellForRowAtIndexPath:indexPath];
    [self.btn setTitle:ce.textLabel.text forState:UIControlStateNormal];
    [self.delegate releaseValuePost:self];
    [self.delegate btnTitle:ce.textLabel.text];
    
}

@end
